package charazay.tool.managers.XML;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import charazay.tool.cobjects.Player;
import charazay.tool.managers.XML.interfaces.PlayerInterface;
import charazay.tool.managers.files.FileManager;

public class PlayerXMLWriter implements PlayerInterface{

	Document doc;
	Element basic;
	Element skills;
	Element status;
	Player player;

	public PlayerXMLWriter(Player player) {
		this.player = player;
	}

	public void write() {
		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			// root elements
			doc = docBuilder.newDocument();
			Element rootElement = doc.createElement(PLAYER);
			doc.appendChild(rootElement);

			rootElement.setAttribute(ID,
					String.valueOf(player.getCharazay_id()));
			rootElement.setAttribute("transfer", "");
			rootElement.setAttribute("nt", "");
			rootElement.setAttribute("u21nt", "");
			rootElement.setAttribute("u18nt", "");
			rootElement.setAttribute("promoted_on", "");
			rootElement.setAttribute("dl", "");
			rootElement.setAttribute(COUNTRY, String.valueOf(player.getCountry()));
			rootElement.setAttribute(
					TEAM,
					String.valueOf(player.getTeam()
							.getCharazay_id()));
			rootElement.setAttribute("ntcap", "");
			rootElement.setAttribute("u21cap", "");
			rootElement.setAttribute("u19cap", "");
			
			// basic elements
			basic = doc.createElement(BASIC);
			rootElement.appendChild(basic);

			basic = addNode(NAME, basic, player.firstName);
			basic = addNode(SURNAME, basic, player.lastName);
			basic = addNode(AGE, basic, String.valueOf(player.age));
			basic = addNode(HEIGHT, basic, String.valueOf(player.height));
			basic = addNode(WEIGHT, basic, String.valueOf(player.weight));

			// skills elements
			skills = doc.createElement(SKILLS);
			rootElement.appendChild(skills);

			skills = addNode(DEFENCE, skills,
					String.valueOf(player.getDefence()));
			skills = addNode(FREETHROW, skills,
					String.valueOf(player.getFreeThrow()));
			skills = addNode(TWOPOINT, skills,
					String.valueOf(player.getTwoPts()));
			skills = addNode(THREEPOINT, skills,
					String.valueOf(player.getThreePts()));
			skills = addNode(DRIBLING, skills,
					String.valueOf(player.getDribbling()));
			skills = addNode(PASSING, skills,
					String.valueOf(player.getPassing()));
			skills = addNode(SPEED, skills, String.valueOf(player.getSpeed()));
			skills = addNode(FOOTWORK, skills,
					String.valueOf(player.getFootwork()));
			skills = addNode(REBOUND, skills,
					String.valueOf(player.getRebond()));
			skills = addNode(XP, skills,
					String.valueOf(player.getExperience()));

			// status elements
			status = doc.createElement(STATUS);
			rootElement.appendChild(status);

			status = addNode(INJURIED, status, "");
			status = addNode(FATIGUE, status, "");
			status = addNode(FAME, status, "");
			status = addNode(SI, status, String.valueOf(player.getSi()));
			status = addNode(SALARY, status,
					String.valueOf(player.getSalary()));

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(
					FileManager.PLAYERS_DIR
							+ String.valueOf(player.getCharazay_id()) + ".xml"));

			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
	}

	public Element addNode(String elementname, Element mainelement, String value) {
		Element element = doc.createElement(elementname);
		mainelement.appendChild(element);
		element.appendChild(doc.createTextNode(value));

		return mainelement;
	}
}
